package weekthree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class NNodeTreePreOrder_589 {

    public List<Integer> preorder(Node root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) return list;
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node head = stack.pop();
            list.add(head.val);
            for (int i = head.children.size() - 1; i >= 0; i--) {
                if (head.children.get(i) != null)
                    stack.push(head.children.get(i));
            }
        }
        return list;
    }

    private class Node{
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }
}
